import request from "@/utils/request"
import local from "@/utils/local"

// import {atob} from 'js-base64'

interface upResult {
    upToken: string
    dir: string
}

//解析upToken的过期时间
const getTokenExpire = (token: string) => {
    try {
        // 提取最后一个冒号后面的值
        const lastColonIndex = token.lastIndexOf(":")
        const encodedValue = token.substring(lastColonIndex + 1)

        // 解码 base64 编码的字符串
        const decodedValue = atob(encodedValue)

        // 将解码后的字符串转换为 JSON 对象
        const jsonValue = JSON.parse(decodedValue)

        return (jsonValue?.deadline || 0) * 1000
    } catch (e) {
        return Date.now() + 1000 * 60 * 60
    }
}

export const getUpToken = async () => {
    const upTokenData = local.getItem("qiniu_upToken")
    if (upTokenData) {
        return upTokenData
    }
    const res = await request("/creatorVideo/getUpToken", "GET")
    if (res.success) {
        const data = res.data as upResult
        local.setItem("qiniu_upToken", data, getTokenExpire(data.upToken))
        return data
    }
}

//获取国际化配置列表
export const getI18nConfigList = async () => {
    const res = await request("/BaseData/GetI18nConfigDic ", "GET")
    if (res.success) {
        if(typeof res.data === 'string'){
            return JSON.parse(res.data)
        }
        return res.data
    }
}

// 获取手机号所属国家/地区列表
export const getPhoneRegionList = async () => {
    const res = await request("/BaseData/GetRegionList", "GET")
    if (res.success) {
        return res.data
    }
}

//获取流量狗版本配置列表
export const getAccountConfigList = async () => {
    const res = await request("/BaseData/GetAccountConfigList", "GET")
    if (res.success) {
        return res.data
    }
}

